<template>
  <view class="content">
    <van-nav-bar left-text="" :left-arrow="true" customClass="title-box" placeholder fixed @click-left="onClickLeft">
      <template #title>
        <view class="search-page">
          <view class="search-box">
            <van-icon name="search" size="20" />
            <input type="text" class="input-val" v-model="searchVal" placeholder="请输入商品" />
          </view>
          <view class="search-btn" @click="searchHandle">搜索</view>
        </view>
      </template>
    </van-nav-bar>
    <view class="search-cont">
      <view class="history" v-if="historyList.length">
        <view class="history-title">历史搜索<text class="icon-del" @click="clearHandle"></text></view>
        <view class="tag-box">
          <text class="tag-item" v-for="(item, index) in historyList" @click="goGood(item)" :key="index">{{
            item
          }}</text>
        </view>
      </view>
      <view class="hot" v-if="list.length">
        <view class="hot-opt">
          <view class="hot-title">热搜推荐</view>
          <view class="opt-btn" @click="goPage">上热搜</view>
        </view>
        <view class="hot-cont" v-if="tagType == 1">
          <view class="cont-list" @clic="goDetail(item)" v-for="item in list" :key="item.goods_id">
            <image class="img-box" :src="item.image" />
            <view class="good-title">{{ item.title }}</view>
          </view>
        </view>
        <view class="tag-box" v-if="tagType == 2">
          <text class="tag-item" v-for="(item, index) in list" @click="goGood(item)" :key="index">{{ item }}</text>
        </view>
      </view>
    </view>
  </view>
</template>

<script>
import { reactive, toRefs, onMounted } from 'vue'
import TitlePage from '@/components/title/index'
import { getHotSearch } from '@/http/common'
export default {
  components: {
    TitlePage,
  },
  setup() {
    const state = reactive({
      list: [],
      tagType: '',
      searchVal: '',
      historyList: [],
    })
    const onClickLeft = () => {
      uni.navigateBack({
        delta: 1,
      })
    }
    const goPage = () => {
      uni.navigateTo({
        url: `/subpackage-platform/hot/index`,
      })
    }
    const goDetail = (item) => {
      uni.navigateTo({
        url: `/subpackage-goods/detail/index?id=${item.goods_id}`,
      })
    }
    const getHot = async () => {
      const { data } = await getHotSearch()
      state.tagType = data.type
      state.list = data.list
    }
    const searchHandle = () => {
      state.historyList.push(state.searchVal)
      state.historyList = [...new Set(state.historyList)]
      uni.setStorageSync('history', JSON.stringify(state.historyList))
      goGood(state.searchVal)
    }
    const goGood = (name) => {
      uni.setStorageSync('searchVal', name)
      uni.switchTab({
        url: `/pages/goods/index`,
      })
    }
    const clearHandle = () => {
      state.historyList = []
      uni.removeStorageSync('history')
    }
    onMounted(() => {
      state.searchVal = ''
      if (uni.getStorageSync('history')) {
        state.historyList = JSON.parse(uni.getStorageSync('history'))
      }
      console.log(uni.getStorageSync('searchVal'))
      if (uni.getStorageSync('searchVal')) {
        state.searchVal = uni.getStorageSync('searchVal')
      }
      getHot()
    })
    return {
      ...toRefs(state),
      onClickLeft,
      goPage,
      goDetail,
      searchHandle,
      clearHandle,
      goGood,
    }
  },
}
</script>
<style lang="scss">
.content {
  height: 100vh;
  background: #fff;
}
.detail {
  height: calc(100vh - 230rpx);
  overflow-y: auto;
}
.search-page {
  position: relative;
  width: 392rpx;
  height: 64rpx;
  padding: 0 24rpx;
  background: #f5f6fa;
  border-radius: 40rpx;
  font-size: 24rpx;
  color: #7f8798;
  line-height: 52rpx;

  .search-box {
    height: 100%;
    display: flex;
    align-items: center;
    color: #2b303b;
    font-size: 28rpx;
  }
  .input-val {
    text-align: left;
    color: #2b303b;
    font-size: 28rpx;
  }
  .search-btn {
    display: block;
    position: absolute;
    right: 10rpx;
    top: 4rpx;
    width: 112rpx;
    height: 56rpx;
    line-height: 56rpx;
    text-align: center;
    font-size: 24rpx;
    color: #fff;
    z-index: 9;
    background: linear-gradient(90deg, #38acff 0%, #387bff 100%);
    border-radius: 28rpx;
  }
}
.search-cont {
  padding: 24rpx;
  .history {
    .history-title {
      font-size: 32rpx;
      font-weight: 500;
      color: #2b303b;
      line-height: 48rpx;
      text {
        display: inline-block;
        vertical-align: middle;
        width: 30rpx;
        height: 30rpx;
        margin-left: 16rpx;
        background: url('https://images.tongzhuangkc.com/tz/delete.png') no-repeat;
        background-size: 100% 100%;
      }
    }
  }
  .hot {
    .hot-opt {
      display: flex;
      align-items: center;
      justify-content: space-between;
      margin-bottom: 32rpx;
      .hot-title {
        font-size: 32rpx;
        font-weight: 500;
        color: #2b303b;
        line-height: 48rpx;
      }
      .opt-btn {
        width: 104rpx;
        font-size: 24rpx;
        line-height: 48rpx;
        text-align: center;
        height: 48rpx;
        color: #fff;
        background: linear-gradient(90deg, #ff92a0 0%, #ee4765 100%);
        box-shadow: 0rpx 4rpx 8rpx 0rpx rgba(241, 83, 111, 0.3);
        border-radius: 32rpx;
      }
    }
    .hot-cont {
      display: flex;
      margin: 0 -11rpx -32rpx;
      .cont-list {
        margin: 0 11rpx 32rpx;
        display: flex;
        .img-box {
          width: 128rpx;
          height: 128rpx;
          border-radius: 16rpx;
          margin-right: 16rpx;
        }
        .good-title {
          width: calc(100% - 144rpx);
          font-size: 24rpx;
          font-weight: 500;
          line-height: 32rpx;
          height: 64rpx;
          @include text-ellipsis(2);
        }
      }
    }
  }
  .tag-box {
    margin: 32rpx -12rpx;
    overflow: hidden;
    .tag-item {
      margin: 0 12rpx 24rpx;
      font-size: 28rpx;
      color: #555b66;
      padding: 0 24rpx;
      height: 64rpx;
      line-height: 64rpx;
      background: #f5f6fa;
      border-radius: 32rpx;
    }
  }
}
</style>
